package com.ruoyi.fund.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fund.domain.DmsFund;
import com.ruoyi.fund.domain.DmsFundRecord;
import com.ruoyi.fund.mapper.DmsFundRecordMapper;
import com.ruoyi.fund.service.IDmsFundRecordService;
import com.ruoyi.fund.service.IDmsFundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 资金记录
Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-06
 */
@Service
public class DmsFundRecordServiceImpl extends ServiceImpl<DmsFundRecordMapper, DmsFundRecord> implements IDmsFundRecordService
{
    @Autowired
    private DmsFundRecordMapper dmsFundRecordMapper;

    @Autowired
    private AutofillUtil autofillUtil;

    @Autowired
    private IDmsFundService iDmsFundService;

    /**
     * 查询资金记录
     * 
     * @param id 资金记录主键
     * @return 资金记录
     */
    @Override
    public DmsFundRecord selectDmsFundRecordById(Long id)
    {
        return dmsFundRecordMapper.selectDmsFundRecordById(id);
    }

    /**
     * 查询资金记录列表
     * 
     * @param dmsFundRecord 资金记录
     * @return 资金记录
     */
    @Override
    public List<DmsFundRecord> selectDmsFundRecordList(DmsFundRecord dmsFundRecord)
    {
        return dmsFundRecordMapper.selectDmsFundRecordList(dmsFundRecord);
    }

    /**
     * 新增资金记录
     * 
     * @param dmsFundRecord 资金记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDmsFundRecord(DmsFundRecord dmsFundRecord)
    {
        //必须选择自己的账户进行操作
//        Long parentId = SecurityUtils.getLoginUser().getUser().getDept().getParentId();
//        if (!dmsFundRecord.getFundOwnerId().equals(parentId)){
//            throw new ServiceException("请选择自己公司的账户！");
//        }

        //判断资金方id 名称 资金所属方是否一致
        if( !(
            (dmsFundRecord.getFundOwnerId()==201 && dmsFundRecord.getFundOwnerName()==0 && dmsFundRecord.getFundOwnerType()==0) ||
                    (dmsFundRecord.getFundOwnerId()==102 && dmsFundRecord.getFundOwnerName()==2 && dmsFundRecord.getFundOwnerType()==1) ||
                    (dmsFundRecord.getFundOwnerId()==101 && dmsFundRecord.getFundOwnerName()==1 && dmsFundRecord.getFundOwnerType()==1)
        ) ){
            throw new ServiceException("资金方id、资金方名称、资金所属方 必须信息一致！");
        }
        //只能消费或者充值(且必须二选一）
        if(!((dmsFundRecord.getConsumerFund()!=0 && dmsFundRecord.getRechargeAmount()==0) || (dmsFundRecord.getConsumerFund()==0 && dmsFundRecord.getRechargeAmount()!=0) )){
            throw new ServiceException("只能选择充值或者消费金额！");
        }
        //判断消费金额和充值金额都大于0
        if (dmsFundRecord.getConsumerFund()<0 || dmsFundRecord.getRechargeAmount()<0){
            throw new ServiceException("充值或者消费金额必须大于0");
        }

        //新增自动填充
        autofillUtil.autoFillCreate(dmsFundRecord);
        //填充消费人、消费时间、消费人id
        dmsFundRecord.setConsumerId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsFundRecord.setConsumerName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsFundRecord.setConsumerTime(new Date());
        dmsFundRecordMapper.insertDmsFundRecord(dmsFundRecord);

        //去资金表里添加相应金额
        //①查询资金表的资金
        DmsFund dmsFund = new DmsFund();
        dmsFund.setFundOwnerId(dmsFundRecord.getFundOwnerId());
        dmsFund.setFundOwnerName(dmsFundRecord.getFundOwnerName());
        dmsFund.setFundOwnerType(dmsFundRecord.getFundOwnerType());
        List<DmsFund> dmsFundList = iDmsFundService.selectDmsFundList(dmsFund);
        //②如果资金表里没有，则代表账户金额为0，需要创建资金表
        if (dmsFundList.size()==0){
            new Thread(() -> {
                iDmsFundService.insertDmsFund(dmsFund);
            }).start();
            throw new ServiceException("尚未创建资金账户，请确认创建后再消费/充值金额！");
        }
        //有资金表时，直接修改金额
        else if (dmsFundList.size()==1){
            //获取资金表的金额
            DmsFund fund = dmsFundList.get(0);
            //消费时
            if (dmsFundRecord.getConsumerFund()>0){
                //检验消费不能超过账户已有金额
                if((fund.getFund()-dmsFundRecord.getConsumerFund())<0){
                    throw new ServiceException("消费金额大于账户金额！");
                }
                fund.setFund(fund.getFund()-dmsFundRecord.getConsumerFund());
            }
            //充值时
            if (dmsFundRecord.getRechargeAmount()>0){
                fund.setFund(fund.getFund()+dmsFundRecord.getRechargeAmount());
            }
            //更新
            iDmsFundService.updateDmsFund(fund);
        }
        else{
            throw new ServiceException("您存在多个资金账户，请联系管理员删除！");
        }
        return 1;
    }

    /**
     * 修改资金记录
     * 
     * @param dmsFundRecord 资金记录
     * @return 结果
     */
    @Override
    public int updateDmsFundRecord(DmsFundRecord dmsFundRecord)
    {
        dmsFundRecord.setUpdateTime(DateUtils.getNowDate());
        return dmsFundRecordMapper.updateDmsFundRecord(dmsFundRecord);
    }

    /**
     * 批量删除资金记录
     * 
     * @param ids 需要删除的资金记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDmsFundRecordByIds(Long[] ids)
    {
        //去资金表里删除对应金额


        return dmsFundRecordMapper.deleteDmsFundRecordByIds(ids);
    }

    /**
     * 删除资金记录信息
     * 
     * @param id 资金记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsFundRecordById(Long id)
    {
        //去资金表里删除对应金额

        return dmsFundRecordMapper.deleteDmsFundRecordById(id);
    }
}
